
import Vue from 'vue'
import VueRouter from 'vue-router'
import auth from '@/utils/auth'
// 导入组件
const Login = () => import('@/views/login')
const Layout = () => import('@/views/Layout')
const Welcome = () => import('@/views/welcome')
const NotFound = () => import('@/views/404')
const Article = () => import('@/views/article')
const Image = () => import('@/views/image')
const Publish = () => import('@/views/publish')
const Comment = () => import('@/views/comment')
const Fans = () => import('@/views/fans')
const Setting = () => import('@/views/setting')
Vue.use(VueRouter)

const routes = [
    {
        path: '/login', component: Login
    },
    {
        path: '/', component: Layout,
        children: [
            {
                path: '/', component: Welcome
            },
            {
                path: '/article', component: Article
            },
            {
                path: '/image', component: Image
            },
            {
                path: '/publish', component: Publish
            },
            {
                path: '/comment', component: Comment
            },
            {
                path: '/fans', component: Fans
            },
            {
                path: '/setting', component: Setting
            }
        ]
    },
    { path: '*', component: Notfound }
]
const router = new VueRouter({
    routes
})
// 导航守卫（每次跳转路由的时候执行）
router.beforeEach((to, from, next) => {
    // to 即将跳转的路由对象
    // from 正在来开的路由对象
    // next() 下一步,放行  next('地址') 拦截，跳转去哪
    const user = auth.getUser()
    // 不是访问登录，且没有token信息，不能访问其他页面，拦截到登录
    if (to.path !== '/login' && !user.token) return next('/login')
    // 其他情况一概放行
    next()
})
export default router